<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="baidu-site-verification" content="zD47G9xe42" />


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="PHP," />





  <link rel="alternate" href="/atom.xml" title="uiste" type="application/atom+xml" />




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.1" />






<meta name="description" content="思考：A页面中如何访问B页面中的变量包含文件get传递或post传递cookie：cookie是小的信息包。

Cookiecookie的原理浏览器—-http请求（无值）——–&amp;gt;服务器浏览器&amp;lt;—响应头中传递cookie——-服务器浏览器—-再次发送http请求（有值）–&amp;gt;服务器

请求头：客户端请求服务器的时候，告知服务器客户端的信息
响应头：服务器响应客户端的时候，告知客户">
<meta property="og:type" content="article">
<meta property="og:title" content="COOKIE 和 SESSION">
<meta property="og:url" content="http://blog.uiste.com/20160926-1.html">
<meta property="og:site_name" content="uiste">
<meta property="og:description" content="思考：A页面中如何访问B页面中的变量包含文件get传递或post传递cookie：cookie是小的信息包。

Cookiecookie的原理浏览器—-http请求（无值）——–&amp;gt;服务器浏览器&amp;lt;—响应头中传递cookie——-服务器浏览器—-再次发送http请求（有值）–&amp;gt;服务器

请求头：客户端请求服务器的时候，告知服务器客户端的信息
响应头：服务器响应客户端的时候，告知客户">
<meta property="og:updated_time" content="2016-09-26T16:48:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="COOKIE 和 SESSION">
<meta name="twitter:description" content="思考：A页面中如何访问B页面中的变量包含文件get传递或post传递cookie：cookie是小的信息包。

Cookiecookie的原理浏览器—-http请求（无值）——–&amp;gt;服务器浏览器&amp;lt;—响应头中传递cookie——-服务器浏览器—-再次发送http请求（有值）–&amp;gt;服务器

请求头：客户端请求服务器的时候，告知服务器客户端的信息
响应头：服务器响应客户端的时候，告知客户">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: 0,
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://blog.uiste.com/20160926-1.html"/>

  <title>
 COOKIE 和 SESSION | uiste 
</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  



  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?b58b87af012832b8ae35e33bb45b44fa";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>








  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">uiste</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">uiste个人博客</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      

            <li class="menu-item menu-item-search"> <a title="把这个链接拖到你的Chrome收藏夹工具栏中" href='javascript:(function() {
    function c() {
        var e = document.createElement("link");
        e.setAttribute("type", "text/css");
        e.setAttribute("rel", "stylesheet");
        e.setAttribute("href", f);
        e.setAttribute("class", l);
        document.body.appendChild(e)
    }
 
    function h() {
        var e = document.getElementsByClassName(l);
        for (var t = 0; t < e.length; t++) {
            document.body.removeChild(e[t])
        }
    }
 
    function p() {
        var e = document.createElement("div");
        e.setAttribute("class", a);
        document.body.appendChild(e);
        setTimeout(function() {
            document.body.removeChild(e)
        }, 100)
    }
 
    function d(e) {
        return {
            height : e.offsetHeight,
            width : e.offsetWidth
        }
    }
 
    function v(i) {
        var s = d(i);
        return s.height > e && s.height < n && s.width > t && s.width < r
    }
 
    function m(e) {
        var t = e;
        var n = 0;
        while (!!t) {
            n += t.offsetTop;
            t = t.offsetParent
        }
        return n
    }
 
    function g() {
        var e = document.documentElement;
        if (!!window.innerWidth) {
            return window.innerHeight
        } else if (e && !isNaN(e.clientHeight)) {
            return e.clientHeight
        }
        return 0
    }
 
    function y() {
        if (window.pageYOffset) {
            return window.pageYOffset
        }
        return Math.max(document.documentElement.scrollTop, document.body.scrollTop)
    }
 
    function E(e) {
        var t = m(e);
        return t >= w && t <= b + w
    }
 
    function S() {
        var e = document.createElement("audio");
        e.setAttribute("class", l);
        e.src = i;
        e.loop = false;
        e.addEventListener("canplay", function() {
            setTimeout(function() {
                x(k)
            }, 500);
            setTimeout(function() {
                N();
                p();
                for (var e = 0; e < O.length; e++) {
                    T(O[e])
                }
            }, 15500)
        }, true);
        e.addEventListener("ended", function() {
            N();
            h()
        }, true);
        e.innerHTML = " <p>If you are reading this, it is because your browser does not support the audio element. We recommend that you get a new browser.</p> <p>";
        document.body.appendChild(e);
        e.play()
    }
 
    function x(e) {
        e.className += " " + s + " " + o
    }
 
    function T(e) {
        e.className += " " + s + " " + u[Math.floor(Math.random() * u.length)]
    }
 
    function N() {
        var e = document.getElementsByClassName(s);
        var t = new RegExp("\\b" + s + "\\b");
        for (var n = 0; n < e.length; ) {
            e[n].className = e[n].className.replace(t, "")
        }
    }
 
    var e = 30;
    var t = 30;
    var n = 350;
    var r = 350;
    var i = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake.mp3";
    var s = "mw-harlem_shake_me";
    var o = "im_first";
    var u = ["im_drunk", "im_baked", "im_trippin", "im_blown"];
    var a = "mw-strobe_light";
    var f = "//s3.amazonaws.com/moovweb-marketing/playground/harlem-shake-style.css";
    var l = "mw_added_css";
    var b = g();
    var w = y();
    var C = document.getElementsByTagName("*");
    var k = null;
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            if (E(A)) {
                k = A;
                break
            }
        }
    }
    if (A === null) {
        console.warn("Could not find a node of the right size. Please try a different page.");
        return
    }
    c();
    S();
    var O = [];
    for (var L = 0; L < C.length; L++) {
        var A = C[L];
        if (v(A)) {
            O.push(A)
        }
    }
})()    '>High</a> </li>

    </ul>
  

 
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
   
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                COOKIE 和 SESSION
              
            
          </h1>
        

        <div class="post-meta">
        
  <span id="busuanzi_container_page_pv">阅读量 <span id="busuanzi_value_page_pv"></span> 次</span>

          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-09-26T19:31:59+08:00" content="2016-09-26">
              2016-09-26
            </time>
          </span>

          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/20160926-1.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="20160926-1.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          

          
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p>思考：A页面中如何访问B页面中的变量<br>包含文件<br>get传递或post传递<br>cookie：cookie是小的信息包。</p>
</blockquote>
<h1 id="Cookie"><a href="#Cookie" class="headerlink" title="Cookie"></a>Cookie</h1><h3 id="cookie的原理"><a href="#cookie的原理" class="headerlink" title="cookie的原理"></a>cookie的原理</h3><p>浏览器—-http请求（无值）——–&gt;服务器<br>浏览器&lt;—响应头中传递cookie——-服务器<br>浏览器—-再次发送http请求（有值）–&gt;服务器</p>
<ol>
<li><strong>请求头：客户端请求服务器的时候，告知服务器客户端的信息</strong></li>
<li><strong>响应头：服务器响应客户端的时候，告知客户端服务器的信息</strong></li>
</ol>
<h3 id="长连接"><a href="#长连接" class="headerlink" title="长连接"></a>长连接</h3><p>在多个http请求之间，TCP连接不断开称为长连接，默认是5秒。<br>长连接过程中，可以建立多次<em>http请求</em>与<em>http响应</em></p>
<h3 id="cookie保存在客户端"><a href="#cookie保存在客户端" class="headerlink" title="cookie保存在客户端"></a>cookie保存在客户端</h3><h3 id="设置cookie"><a href="#设置cookie" class="headerlink" title="设置cookie"></a>设置cookie</h3><p>语法：setcookie(名字,值,过期时间,有效目录,子域名)</p>
<p><code>bool setcookie ( string $name [, string $value = &quot;&quot; [, int $expire = 0 [, string $path = &quot;&quot; [, string $domain = &quot;&quot; [, bool $secure = false [, bool $httponly = false ]]]]]] )</code></p>
<h3 id="临时性cookie"><a href="#临时性cookie" class="headerlink" title="临时性cookie"></a>临时性cookie</h3><p>关闭浏览器cookie就消失</p>
<h3 id="永久性cookie"><a href="#永久性cookie" class="headerlink" title="永久性cookie"></a>永久性cookie</h3><ul>
<li>[ ] 下次自动登录    （案例）</li>
</ul>
<blockquote>
<p>在临时性cookie的基础上加上一个过期时间就成了永久性cookie，过期时间是一个时间戳<br>永久性cookie保存在浏览器上，换个浏览器就不行了</p>
</blockquote>
<h3 id="cookie保存的数据类型"><a href="#cookie保存的数据类型" class="headerlink" title="cookie保存的数据类型"></a>cookie保存的数据类型</h3><p>cookie不能存放bool型数据，true=&gt;1 false=&gt;delete<br>cookie不能存放数组和对象<br><strong>cookie中只能存放字符串</strong></p>
<h3 id="删除cookie"><a href="#删除cookie" class="headerlink" title="删除cookie"></a>删除cookie</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">Set-Cookie:</div><div class="line">name=uiste; expires=Mon, 26-Sep-2016 12:07:11 GMT; Max-Age=100</div><div class="line">sex=man; expires=Mon, 26-Sep-2016 12:07:11 GMT; Max-Age=100</div><div class="line">age=24; expires=Mon, 26-Sep-2016 12:07:11 GMT; Max-Age=100</div><div class="line">name=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0</div><div class="line">sex=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0</div><div class="line">age=</div></pre></td></tr></table></figure>
<h3 id="cookie支持下标"><a href="#cookie支持下标" class="headerlink" title="cookie支持下标"></a>cookie支持下标</h3><p>cookie在浏览器上没有数组的概念，只是浏览器请求到服务器，PHP将同名的变量转成数组</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">//cookie不支持数组，但是支持下标</div><div class="line">setcookie(&apos;stu[0]&apos;,&apos;tom&apos;);</div><div class="line">setcookie(&apos;stu[1]&apos;,&apos;berry&apos;);</div><div class="line">setcookie(&apos;stu[2]&apos;,&apos;ketty&apos;);</div><div class="line">?&gt;</div></pre></td></tr></table></figure>
<h3 id="cookie有效目录"><a href="#cookie有效目录" class="headerlink" title="cookie有效目录"></a>cookie有效目录</h3><p>默认情况下，cookie只能在当前目录和子级目录中有效<br>如果要在整个网站中有效，就要设置有效目录，‘/’表示整站有效<br><code>setcookie(&#39;name&#39;,&#39;tom&#39;,0,&#39;/&#39;)</code></p>
<h3 id="cookie支持子域名"><a href="#cookie支持子域名" class="headerlink" title="cookie支持子域名"></a>cookie支持子域名</h3><p>默认情况下，cookie是区分子域名的。一个子域名就代替一个独立的服务器</p>
<h3 id="cookie的缺点"><a href="#cookie的缺点" class="headerlink" title="cookie的缺点"></a>cookie的缺点</h3><ol>
<li>信息保存在本地，安全性低</li>
<li>只能保存字符串，不能保存数组和对象，可控性差</li>
<li>数据保存在头信息中，增加请求时的数据负载</li>
<li>一般浏览器对cookie的要求是不能高于4K</li>
</ol>
<h1 id="session-会话"><a href="#session-会话" class="headerlink" title="session(会话)"></a>session(会话)</h1><p>将数据存储到服务器端，每个客户一个空间，通过编号访问自己的数据</p>
<ul>
<li>第一次访问服务器，服务器给客户端分配一个编号</li>
<li>通过编号来访问服务器保存的数据</li>
<li>每个客户只能访问自己的数据<br><strong>session是基于cookie的技术</strong></li>
</ul>
<h3 id="session的操作"><a href="#session的操作" class="headerlink" title="session的操作"></a>session的操作</h3><ol>
<li>要使用会话必须显示的开启会话，<code>session_start()</code>。默认情况下，会话不会自动开启。</li>
<li>要自动开启会话，在<code>php.ini</code>文件中<code>session.auto_start=1</code>，把0改为1就自动开启了</li>
<li>session_id():会话编号</li>
<li>session_name():会话名称</li>
<li>session可以保存除了资源以外的所有类型的数据</li>
<li>重复开启会话会报错，可以通过<code>@</code>来屏蔽会话</li>
</ol>
<h3 id="session执行过程"><a href="#session执行过程" class="headerlink" title="session执行过程"></a>session执行过程</h3><ol>
<li>使用<code>session_start()</code>从<code>session</code>仓库中加载已经存在的<code>session</code>变量</li>
<li>第一次请求服务器，一个唯一的会话id存储到cookie响应头中</li>
<li>在后面的请求中，会话id保存请求头中，服务器获取请求头的会话id，通过id获取会话的值</li>
<li>页面执行完毕返回到客户端之前，将会话自动保存到服务器的session库中。下次浏览网页可以加载再次使用</li>
</ol>
<h3 id="session的配置"><a href="#session的配置" class="headerlink" title="session的配置"></a>session的配置</h3><h2 id="1-session-save-path：指定session库的路径"><a href="#1-session-save-path：指定session库的路径" class="headerlink" title="1. session.save_path：指定session库的路径  "></a>1. session.save_path：指定session库的路径  </h2><p>session.save_path = “N;/path”, N表示通过N级目录来保存会话，N等于1，就是通过会话的第一个字母创建文件夹；N等于2表示通过两级目录创建文件夹，第一级是第一个字母，第二级是第二个字母。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">; 用于保存/取回数据的控制方式</div><div class="line">session.save_handler=files</div><div class="line">;     session.save_path = &quot;N;/path&quot;</div><div class="line">; 在 save_handler 设为文件时传给控制器的参数， 这是数据文件将保存的路径</div><div class="line">session.save_path=&quot;/Applications/XAMPP/xamppfiles/temp/&quot;</div></pre></td></tr></table></figure>
<h2 id="2-session-name：设置会话名称"><a href="#2-session-name：设置会话名称" class="headerlink" title="2. session.name：设置会话名称"></a>2. session.name：设置会话名称</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">; Name of the session (used as cookie name).</div><div class="line">; http://php.net/session.name</div><div class="line">session.name=PHPSESSID</div></pre></td></tr></table></figure>
<h2 id="3-session-cookie-lifetime：保存会话编号的cookie有效时间"><a href="#3-session-cookie-lifetime：保存会话编号的cookie有效时间" class="headerlink" title="3. session.cookie_lifetime：保存会话编号的cookie有效时间"></a>3. session.cookie_lifetime：保存会话编号的cookie有效时间</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">; Lifetime in seconds of cookie or, if 0, until browser is restarted.</div><div class="line">; http://php.net/session.cookie-lifetime</div><div class="line">session.cookie_lifetime=0</div></pre></td></tr></table></figure>
<h2 id="4-session-cookie-path：保存会话的cookie整站有效"><a href="#4-session-cookie-path：保存会话的cookie整站有效" class="headerlink" title="4. session.cookie_path：保存会话的cookie整站有效"></a>4. session.cookie_path：保存会话的cookie整站有效</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">; The path for which the cookie is valid.</div><div class="line">; http://php.net/session.cookie-path</div><div class="line">session.cookie_path=/</div></pre></td></tr></table></figure>
<h2 id="5-session-cookie-domain：当前域名有效"><a href="#5-session-cookie-domain：当前域名有效" class="headerlink" title="5. session.cookie_domain：当前域名有效"></a>5. session.cookie_domain：当前域名有效</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">; The domain for which the cookie is valid.</div><div class="line">; http://php.net/session.cookie-domain</div><div class="line">session.cookie_domain=</div></pre></td></tr></table></figure>
<h2 id="6-session-save-handler-files：会话按文件格式保存"><a href="#6-session-save-handler-files：会话按文件格式保存" class="headerlink" title="6. session.save_handler = files：会话按文件格式保存"></a>6. session.save_handler = files：会话按文件格式保存</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">; Handler used to store/retrieve data.</div><div class="line">; http://php.net/session.save-handler</div><div class="line">session.save_handler=files</div></pre></td></tr></table></figure>
<h2 id="7-session-hash-function-0：设置session会话的算法"><a href="#7-session-hash-function-0：设置session会话的算法" class="headerlink" title="7. session.hash_function = 0：设置session会话的算法"></a>7. session.hash_function = 0：设置session会话的算法</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">; Select a hash function for use in generating session ids.</div><div class="line">; Possible Values </div><div class="line">;   0  (MD5 128 bits)</div><div class="line">;   1  (SHA-1 160 bits)</div><div class="line">; http://php.net/session.hash-function</div><div class="line">session.hash_function=0</div></pre></td></tr></table></figure>
<h2 id="8-session-hash-bits-per-character-5：每个字符占用的位数"><a href="#8-session-hash-bits-per-character-5：每个字符占用的位数" class="headerlink" title="8. session.hash_bits_per_character = 5：每个字符占用的位数"></a>8. session.hash_bits_per_character = 5：每个字符占用的位数</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">; Define how many bits are stored in each character when converting</div><div class="line">; the binary hash data to something readable.</div><div class="line">; Possible values:</div><div class="line">;   4  (4 bits: 0-9, a-f)</div><div class="line">;   5  (5 bits: 0-9, a-v)</div><div class="line">;   6  (6 bits: 0-9, a-z, A-Z, &quot;-&quot;, &quot;,&quot;)</div><div class="line">; Default Value: 4</div><div class="line">; Development Value: 5</div><div class="line">; Production Value: 5</div><div class="line">; http://php.net/session.hash-bits-per-character</div><div class="line">session.hash_bits_per_character=5</div></pre></td></tr></table></figure>
<h2 id="9-session-gc-maxlifetime-1440：会话过期时间"><a href="#9-session-gc-maxlifetime-1440：会话过期时间" class="headerlink" title="9. session.gc_maxlifetime = 1440：会话过期时间"></a>9. session.gc_maxlifetime = 1440：会话过期时间</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">; After this number of seconds, stored data will be seen as &apos;garbage&apos; and</div><div class="line">; cleaned up by the garbage collection process.</div><div class="line">; http://php.net/session.gc-maxlifetime</div><div class="line">session.gc_maxlifetime=1440</div></pre></td></tr></table></figure>
<h3 id="session-destroy-：销毁会话"><a href="#session-destroy-：销毁会话" class="headerlink" title="session_destroy()：销毁会话"></a>session_destroy()：销毁会话</h3><ol>
<li>删除储存介质中的文件</li>
<li>执行此行，就不会执行会话写入操作了</li>
</ol>
<h1 id="session入库"><a href="#session入库" class="headerlink" title="session入库"></a>session入库</h1><h3 id="新建数据库"><a href="#新建数据库" class="headerlink" title="新建数据库"></a>新建数据库</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">drop table if exists sess;</div><div class="line">create table sess(</div><div class="line">       sess_id varchar(32) primary key comment &apos;会话编号&apos;,</div><div class="line">       sess_value text comment &apos;会话的值&apos;,</div><div class="line">       sess_expires int not null comment &apos;会话产生时间&apos;</div><div class="line">)engine=innodb charset=utf8;</div></pre></td></tr></table></figure>
<h3 id="session-set-save-handler-设置用户自定义会话存储函数"><a href="#session-set-save-handler-设置用户自定义会话存储函数" class="headerlink" title="session_set_save_handler()设置用户自定义会话存储函数"></a>session_set_save_handler()设置用户自定义会话存储函数</h3><p><code>bool session_set_save_handler ( callable $open , callable $close , callable $read , callable $write , callable $destroy , callable $gc [, callable $create_sid ] )</code></p>
<ul>
<li>open:开启会话</li>
<li>close:关闭会话</li>
<li>read:读取会话</li>
<li>write:写入会话</li>
<li>destroy:销毁会话</li>
<li>gc:销毁会话</li>
</ul>
<p>执行session_destroy()的时候才调用destroy()函数</p>
<blockquote>
<p>为什么gc不执行?</p>
</blockquote>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">; Defines the probability that the &apos;garbage collection&apos; process is started</div><div class="line">; on every session initialization. The probability is calculated by using</div><div class="line">; gc_probability/gc_divisor. Where session.gc_probability is the numerator</div><div class="line">; and gc_divisor is the denominator in the equation. Setting this value to 1</div><div class="line">; when the session.gc_divisor value is 100 will give you approximately a 1% chance</div><div class="line">; the gc will run on any give request.</div><div class="line">; Default Value: 1</div><div class="line">; Development Value: 1</div><div class="line">; Production Value: 1</div><div class="line">; http://php.net/session.gc-probability</div><div class="line">session.gc_probability=1</div><div class="line"></div><div class="line">; Defines the probability that the &apos;garbage collection&apos; process is started on every</div><div class="line">; session initialization. The probability is calculated by using the following equation: </div><div class="line">; gc_probability/gc_divisor. Where session.gc_probability is the numerator and</div><div class="line">; session.gc_divisor is the denominator in the equation. Setting this value to 1</div><div class="line">; when the session.gc_divisor value is 100 will give you approximately a 1% chance</div><div class="line">; the gc will run on any give request. Increasing this value to 1000 will give you</div><div class="line">; a 0.1% chance the gc will run on any give request. For high volume production servers,</div><div class="line">; this is a more efficient approach.</div><div class="line">; Default Value: 100</div><div class="line">; Development Value: 1000</div><div class="line">; Production Value: 1000</div><div class="line">; http://php.net/session.gc-divisor</div><div class="line">session.gc_divisor=1000</div></pre></td></tr></table></figure>
<blockquote>
<p>session.gc_probability=1   概率<br>session.gc_divisor=1000    除数<br>执行垃圾回收的概率是：1/1000</p>
</blockquote>
<h3 id="session入库-1"><a href="#session入库-1" class="headerlink" title="session入库"></a>session入库</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div></pre></td><td class="code"><pre><div class="line">&lt;?php</div><div class="line">header(&apos;content-type:text/html;charset=utf-8&apos;);</div><div class="line">//开启会话</div><div class="line">function open() &#123;</div><div class="line">	mysql_connect(&apos;localhost&apos;,&apos;root&apos;,&apos;aa&apos;);</div><div class="line">	mysql_query(&apos;set names utf8&apos;);</div><div class="line">	mysql_query(&apos;use php5&apos;);</div><div class="line">&#125;</div><div class="line">//关闭会话</div><div class="line">function close() &#123;</div><div class="line">&#125;</div><div class="line">/**</div><div class="line">*读取会话</div><div class="line">*@param $sess_id string 会话编号</div><div class="line">*/</div><div class="line">function read($sess_id) &#123;</div><div class="line">	$sql=&quot;select sess_value from sess where sess_id=&apos;$sess_id&apos;&quot;;</div><div class="line">	$rs=mysql_query($sql);</div><div class="line">	if($rows=mysql_fetch_row($rs))</div><div class="line">		return $rows[0];</div><div class="line">	return &apos;&apos;;</div><div class="line">&#125;</div><div class="line">/**</div><div class="line">*写入会话</div><div class="line">*@param $sess_id string 会话编号</div><div class="line">*@param $sess_value string 会话值</div><div class="line">*/</div><div class="line">function write($sess_id,$sess_value) &#123;</div><div class="line">	$expires=time();</div><div class="line">	$sql=&quot;insert into sess values (&apos;$sess_id&apos;,&apos;$sess_value&apos;,$expires) on duplicate key update sess_value=&apos;$sess_value&apos;,sess_expires=$expires&quot;;</div><div class="line">	return mysql_query($sql);</div><div class="line">&#125;</div><div class="line">/**</div><div class="line">*销毁会话</div><div class="line">*/</div><div class="line">function destroy($sess_id) &#123;</div><div class="line">	$sql=&quot;delete from sess where sess_id=&apos;$sess_id&apos;&quot;;</div><div class="line">	return mysql_query($sql);</div><div class="line">&#125;</div><div class="line">/**</div><div class="line">*垃圾回收</div><div class="line">*超过生命周期的会话都是垃圾</div><div class="line">*/</div><div class="line">function gc($maxlifetime) &#123;</div><div class="line">	$time=time()-$maxlifetime;</div><div class="line">	$sql=&quot;delete from sess where sess_expires&lt;$time&quot;;</div><div class="line">	return mysql_query($sql);</div><div class="line">&#125;</div><div class="line"></div><div class="line">//更改会话的存储</div><div class="line">session_set_save_handler(&apos;open&apos;,&apos;close&apos;,&apos;read&apos;,&apos;write&apos;,&apos;destroy&apos;,&apos;gc&apos;);</div><div class="line">session_start();</div><div class="line">$_SESSION[&apos;sex&apos;]=100;</div><div class="line">echo $_SESSION[&apos;sex&apos;];</div><div class="line">session_destroy();</div><div class="line">?&gt;</div></pre></td></tr></table></figure>
<hr>
<blockquote>
<p>session_set_save_handler()必须放在session_start()前面才能起作用</p>
</blockquote>
<h1 id="COOKIE-与-SESSION的区别"><a href="#COOKIE-与-SESSION的区别" class="headerlink" title="COOKIE 与 SESSION的区别"></a>COOKIE 与 SESSION的区别</h1><p>cookie将键值存储在浏览器上<br>session将键存储在浏览器上，值存储在服务器上。</p>
<blockquote>
<p>cookie的可以被禁用的，禁用cookie，基于cookie的所有技术都无法使用。</p>
</blockquote>
<p>默认情况下，session是基于的cookie的，可以通过配置更改。</p>
<p><code>session.use_trans_sid=1</code><br><code>session.use_only_cookies=0</code></p>
<p>重启Apache会发现，会话编号不保存到cookie中，而是保存到get提交的URL中或表单的隐藏域中。</p>
<blockquote>
<p>已知session_set_save_handler()必须在session_start()前面开启，如果在php.ini中设置了session.auto_start = 1后，则session_set_save_handler()无效。如何处理？<br>在分布式部署文件中有两个指令来更改php.ini中的配置<br>php_flag：用来更改开关性质的配置<br>php_value：用来更改值性质的配置<br>php_flag session.auto_start 0<br>php_value session.gc_maxlifetime 10<br>将以上命令添置在.htaccess文件中</p>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/PHP/" rel="tag">#PHP</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/20160924-2.html" rel="next" title="初识Linux">
                <i class="fa fa-chevron-left"></i> 初识Linux
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/20160927-1.html" rel="prev" title="PHP-验证码图片排错">
                PHP-验证码图片排错 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
        <!-- Go to www.addthis.com/dashboard to customize your tools -->
<script type = "text/javascript" src = "//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-57838eee5e2620d0" async = "async" ></script>
      
    </div>
  </div>


          </div>
          

  <p>热评文章</p>
  <div class="ds-top-threads" data-range="weekly" data-num-items="4"></div>


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="20160926-1.html"
           data-title="COOKIE 和 SESSION" data-url="http://blog.uiste.com/20160926-1.html">
      </div>
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="uiste" />
          <p class="site-author-name" itemprop="name">uiste</p>
          <p class="site-description motion-element" itemprop="description">Stay Hungry，Stay Foolish</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">70</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">分类</span>
              
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">55</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        
          <div class="feed-link motion-element">
            <a href="/atom.xml" rel="alternate">
              <i class="fa fa-rss"></i>
              RSS
            </a>
          </div>
        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/uiste" target="_blank" rel="external nofollow" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://weibo.com/uiste" target="_blank" rel="external nofollow" title="微博">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  微博
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="http://www.zhihu.com/people/uiste" target="_blank" rel="external nofollow" title="知乎">
                  
                    <i class="fa fa-fw fa-globe"></i>
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        
          <div class="links-of-blogroll motion-element links-of-blogroll-inline">
            <div class="links-of-blogroll-title">
              <i class="fa  fa-fw fa-globe"></i>
              友情链接
            </div>
            <ul class="links-of-blogroll-list">
              
                <li class="links-of-blogroll-item">
                  <a href="http://www.uiste.com" title="uiste 个人博客" target="_blank">uiste 个人博客</a>
                </li>
              
                <li class="links-of-blogroll-item">
                  <a href="http://uiste.github.io" title="uiste 技术博客" target="_blank">uiste 技术博客</a>
                </li>
              
            </ul>
          </div>
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Cookie"><span class="nav-number">1.</span> <span class="nav-text">Cookie</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#cookie的原理"><span class="nav-number">1.0.1.</span> <span class="nav-text">cookie的原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#长连接"><span class="nav-number">1.0.2.</span> <span class="nav-text">长连接</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cookie保存在客户端"><span class="nav-number">1.0.3.</span> <span class="nav-text">cookie保存在客户端</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置cookie"><span class="nav-number">1.0.4.</span> <span class="nav-text">设置cookie</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#临时性cookie"><span class="nav-number">1.0.5.</span> <span class="nav-text">临时性cookie</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#永久性cookie"><span class="nav-number">1.0.6.</span> <span class="nav-text">永久性cookie</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cookie保存的数据类型"><span class="nav-number">1.0.7.</span> <span class="nav-text">cookie保存的数据类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#删除cookie"><span class="nav-number">1.0.8.</span> <span class="nav-text">删除cookie</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cookie支持下标"><span class="nav-number">1.0.9.</span> <span class="nav-text">cookie支持下标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cookie有效目录"><span class="nav-number">1.0.10.</span> <span class="nav-text">cookie有效目录</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cookie支持子域名"><span class="nav-number">1.0.11.</span> <span class="nav-text">cookie支持子域名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cookie的缺点"><span class="nav-number">1.0.12.</span> <span class="nav-text">cookie的缺点</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#session-会话"><span class="nav-number">2.</span> <span class="nav-text">session(会话)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#session的操作"><span class="nav-number">2.0.1.</span> <span class="nav-text">session的操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#session执行过程"><span class="nav-number">2.0.2.</span> <span class="nav-text">session执行过程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#session的配置"><span class="nav-number">2.0.3.</span> <span class="nav-text">session的配置</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-session-save-path：指定session库的路径"><span class="nav-number">2.1.</span> <span class="nav-text">1. session.save_path：指定session库的路径  </span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-session-name：设置会话名称"><span class="nav-number">2.2.</span> <span class="nav-text">2. session.name：设置会话名称</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-session-cookie-lifetime：保存会话编号的cookie有效时间"><span class="nav-number">2.3.</span> <span class="nav-text">3. session.cookie_lifetime：保存会话编号的cookie有效时间</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-session-cookie-path：保存会话的cookie整站有效"><span class="nav-number">2.4.</span> <span class="nav-text">4. session.cookie_path：保存会话的cookie整站有效</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-session-cookie-domain：当前域名有效"><span class="nav-number">2.5.</span> <span class="nav-text">5. session.cookie_domain：当前域名有效</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-session-save-handler-files：会话按文件格式保存"><span class="nav-number">2.6.</span> <span class="nav-text">6. session.save_handler = files：会话按文件格式保存</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-session-hash-function-0：设置session会话的算法"><span class="nav-number">2.7.</span> <span class="nav-text">7. session.hash_function = 0：设置session会话的算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#8-session-hash-bits-per-character-5：每个字符占用的位数"><span class="nav-number">2.8.</span> <span class="nav-text">8. session.hash_bits_per_character = 5：每个字符占用的位数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#9-session-gc-maxlifetime-1440：会话过期时间"><span class="nav-number">2.9.</span> <span class="nav-text">9. session.gc_maxlifetime = 1440：会话过期时间</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#session-destroy-：销毁会话"><span class="nav-number">2.9.1.</span> <span class="nav-text">session_destroy()：销毁会话</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#session入库"><span class="nav-number">3.</span> <span class="nav-text">session入库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#新建数据库"><span class="nav-number">3.0.1.</span> <span class="nav-text">新建数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#session-set-save-handler-设置用户自定义会话存储函数"><span class="nav-number">3.0.2.</span> <span class="nav-text">session_set_save_handler()设置用户自定义会话存储函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#session入库-1"><span class="nav-number">3.0.3.</span> <span class="nav-text">session入库</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#COOKIE-与-SESSION的区别"><span class="nav-number">4.</span> <span class="nav-text">COOKIE 与 SESSION的区别</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2015 - 
  <span itemprop="copyrightYear">2016</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">uiste</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="http://hexo.io" rel="external nofollow">Hexo</a> 趋势动力
</div>
<!--站长统计-->
<script src="https://s95.cnzz.com/z_stat.php?id=1259894121&web_id=1259894121" language="JavaScript"></script>
<!--<div class="theme-info">
  Theme -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next" rel="external nofollow">
    NexT.Mist
  </a>
</div> -->
&nbsp;&nbsp;&nbsp;本站总点击 <span id="busuanzi_value_site_pv"></span> 次
&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;您是第 <span id="busuanzi_value_site_uv"></span> 位访客

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>

<script>
(function(){
    var bp = document.createElement('script');
    bp.src = '//push.zhanzhang.baidu.com/push.js';
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/vendors/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/vendors/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/vendors/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/vendors/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.1"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"uiste"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/vendors/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = true;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = data.url;
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '' && data_content != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title < 0 && index_content < 0 ){
                                isMatch = false;
                            } else {
                                if (index_content < 0) {
                                    index_content = 0;
                                }
                                if (i == 0) {
                                    first_occur = index_content;
                                }
                            }
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });

                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  

</body>
</html>
